
/****
 * Copyright (C) 2006 dNux Team
 *                                                            ****
 * This program is free software; you can redistribute it and/or *
 * modify under the terms of the GNU General Public License as   *
 * published by the Free Software Foundation;                    *
 * either version 2 of the License.                              *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of without even the implied waranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          *
 * See the GNU General Public License for more details.          *
 *                                                               *
 * You should have received a copy of the                        *
 * GNU General Public License                                    *
 * along with this program; if not, write to the                 *
 * Free Software Foundation, Inc.,                               *
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *                                                               *
 ****                                                         ****/
#include <dcore.h>
#include <dhash.h>

static d_bool_t option_output = d_true;
static d_bool_t option_output_xml = d_false;

static char *option_output_file = NULL;
static char *option_check_file = NULL;

static d_bool_t option_md5 = d_false;
static d_bool_t option_sha1 = d_false;
static d_bool_t option_sha256 = d_false;
static d_bool_t option_sha512 = d_false;

static d_option_t options[] = {
  {"--output-xml",  "-xml", "xml output", D_OPTION_VOID, &option_output_xml},
  {"--output-file", "-o", "save result to file", D_OPTION_STRING,
   &option_output_file},
  {"--check",       "-c", "check from digest file", D_OPTION_STRING,
   &option_check_file},
  {"--hash-md5",    "-md5", "md5 digest method", D_OPTION_VOID, &option_md5},
  {"--hash-sha1",   "-sha1", "sha1 digest method", D_OPTION_VOID, &option_sha1},
  {"--hash-sha256", "-sha256", "sha256 digest method", D_OPTION_VOID, 
   &option_sha256},
  {"--hash-sha512", "-sha512", "sha512 digest method", D_OPTION_VOID,
   &option_sha512},
  {NULL}
};

int
main(int argc, char **argv)
{
  int i;
  d_error_t *error=NULL;
  d_hash_t *hash=NULL;

  if (argc == 1) {
	fprintf(stderr, "see help --help.\n");
	return EXIT_FAILURE;
  }

  d_option_parse(options, &argc, &argv, &error);
  if (error) {
    d_error_display(error);
    d_error_free(&error);
  }

  d_hash_init();
  hash = d_hash_new(&error);
  if (error) {
    d_error_display(error);
    d_error_free(&error);
    return EXIT_FAILURE;
  }
  
  for (i=1; argv[i]; i++) {
    d_hash_add_file(hash, argv[i]);
  }

  if (option_check_file) {
    d_hash_references_load(hash, option_check_file, &error);
    if (error) {
      d_error_display(error);
      d_error_free(&error);
    }

    d_hash_perform_references(hash, &error);
    if (error) {
      d_error_display(error);
      d_error_free(&error);
    }
  }
  else {
    if (option_md5) d_hash_perform_digest(hash, "md5", &error);
    if (option_sha1) d_hash_perform_digest(hash, "sha1", &error);
    if (option_sha256) d_hash_perform_digest(hash, "sha256", &error);
    if (option_sha512) d_hash_perform_digest(hash, "sha512", &error);
  }

  if (error) {
    d_error_display(error);
    d_error_free(&error);
    return EXIT_FAILURE;
  }

  if (option_check_file) {
    d_hash_display_diff(hash);
  }
  else if (option_output_file) {
    d_hash_result_save(hash, option_output_file, &error);
  }
  else if (option_output) {
    if (option_output_xml) {
      d_hash_display_result_xml(hash);
    } else {
      d_hash_display_result(hash);
    }
  }
  d_hash_free(&hash);

  return EXIT_SUCCESS;
}
